#include "TestEnemy2.h"

JumpEnemy::JumpEnemy(const Vector2df p, const Vector2df v) {
    m_walking.loadFrames("images/warioRunning1.png",3,1);
    m_walking.setLooping(true);
    m_walking.setFrameRate(DECI_SECONDS);

    m_sprite = &m_walking;

    m_collision.loadImage("images/warioCollision.png");
    m_collision.setNoCollisionColour(MAGENTA);
    m_acceleration[0] = Vector2df(0.0,0.0);
    m_acceleration[1] = Vector2df(0.0,0.0);
}

JumpEnemy::~JumpEnemy() {

}

void JumpEnemy::move(TileMap* map, Vector2df Offset) {

  // This is mostly copy-pasted from StateMain - for now...

  // Apply Gravity
  m_velocity.y += 2;
  // Make sure its speed is never greater than the tile size as
  // this will cause the collision detection to not behave properly
  if (m_velocity.x > 24)
    m_velocity.x = 24;
  if (m_velocity.y > 24)
    m_velocity.y = 24;

  double dx, dy;
  cout << "Checking enemy collision...";
  // Move unit according to its xvelocity
  m_position.x += m_velocity.x;
  // Move unit so that it is not colliding with any tiles
  map->HandleCharacterCollisions_x((double)m_position.x+Offset.x, (double)m_position.y+Offset.y, (double)m_sprite->getWidth(), (double)m_sprite->getHeight(), dx, dy);
  // If it hits a wall, it starts to walk into the opposite direction
  if (m_velocity.x + dx == 0) {
      m_velocity.x *= -1;
  }
  m_position.x += dx;
  m_position.y += dy;
  if (dy < 0) // Meaning it hit something below it
    m_velocity.y = 0;
  cout << "X done...";
  // Move unit according to its yvelocity
  m_position.y += m_velocity.y;
  // Move unit so that it is not colliding with any tiles
  map->HandleCharacterCollisions_y((double)m_position.x+Offset.x, (double)m_position.y+Offset.y, (double)m_sprite->getWidth(), (double)m_sprite->getHeight(), dx, dy);
  m_position.x += dx;
  m_position.y += dy;

  // If it his something above it stops moving upwards
  if (dy > 0 && m_velocity.y < 0) {
      m_velocity.y = 0;
  }
  // If it hits the ground it jumps again
  if (m_velocity.y + dy == 0 && dy < 0) {
      m_velocity.y = -20;
  }
  cout << "Y done...Done!" << endl;
}
